<?php

declare(strict_types=1);
use function MicroHTML\INPUT;
use function MicroHTML\DIV;
use function MicroHTML\A;
use function MicroHTML\IMG;
use function MicroHTML\VIDEO;
use function MicroHTML\SOURCE;

class CustomFeaturedTheme extends FeaturedTheme 
{
  public function display_featured(Page $page, Image $image): void
    {
        $page->add_block(new Block("Featured Gem", $this->build_featured_html($image), "left", 3));
    }
  public function build_featured_html(Image $image, ?string $query=null): string
    {
        $tsize = get_thumbnail_size($image->width, $image->height);

        $media_src = $image->get_thumb_link(); 
        $is_gif = $image->get_ext() == "gif";
        $is_video = !$is_gif && isset($image->video) && $image->video;
        if($is_gif || $is_video){
          $media_src = $image->get_image_link();
        }

        $media_html = DIV([], "Unsupported format");

        if(!$is_video) {
          $media_html = IMG([
            "id"=>"thumb_rand_{$image->id}",
            "title"=>$image->get_tooltip(),
            "alt"=>$image->get_tooltip(),
            "class"=>'highlighted',
            "style"=>"max-height: {$tsize[1]}px; max-width: 100%;",
            "src"=>$media_src
          ]);
        } else {
          $media_html = VIDEO([
            "style"=>"max-height: {$tsize[1]}px; max-width: 100%;",
            "width"=>$tsize[0],
            "height"=>$tsize[1],
            "muted"=>true,
            "autoplay"=>true,
            "loop"=>true
            ],
            SOURCE([
              "src"=>$media_src,
            ])
          );
        }

        return (string)DIV(
            ["style"=>"text-align: center;"],
            A(
                ["href"=>make_link("post/view/{$image->id}", $query)],
                $media_html
            )
        );
    }
}
